
import _config from './config'; // 导入私有配置
import { authUrl, faceUrl } from "@/common/config/index.js"

function getAccessToken(data, isFace = false) {
	return new Promise((resolve, reject) => {
		if(isFace) resolve()
		uni.request({
			url: authUrl + '/token',
			header: _config.header,
			data,
			method: 'POST',
			complete: res => {
				if (res.statusCode == 200) {
					resolve(res.data.access_token)
				} else {
					reject(res)
				}
			}
		})
	})
}

// 每一次请求都要重新请求 token 值
export default function $http(options) {
    return new Promise((resolve, reject) => {
		// 是否禁用的加载动画
		if (!options.disabledLoading) {
			uni.showLoading({title:'加载中', mask: true})
		}		
		let d = {
			client_id: '198818041200011majie123AM',
			client_secret: 'a7a20785-1ec8-4ec3-bc22-9e6da9cdbbfb1majie1988',
			grant_type: 'client_credentials',
		}
		getAccessToken(d, options.recognition).then(access_token => {				
			// 拦截请求
			_config.complete = (response) => {
				uni.hideLoading()
			    //  request請求访问成功
			    if (response.statusCode === 200) {
			        // 接口请求成功
			        resolve(response);
			    } else {
			        // 处理 catch 请求，不在本页面之外处理，统一在这里处理
			        if (options.handle) {
			            reject(response)
			        } else {
			            try {
			                Promise.reject(response).catch(err => {
			                    _page_error(response.statusCode || response.errMsg)
								 reject(response)
			                });
			            } catch (e) {
			                console.log(e)
			            }
			        }
			    }
			}
			options.header = Object.assign({}, _config.header, options.header)
			
			// 人脸识别
			if (options.recognition) {
				options.url = faceUrl + options.url;
			} else {
				options.url = _config.url + options.url;
				options.header.Authorization = 'Bearer ' + access_token;
			}		
			
			
			console.log('%c---------------- 每一次请求前携带的token值 --------------', 'color:blue')
			console.log(options.header.Authorization)
			console.log('%c----------------------------------------------------', 'color:blue')
			
			// 开始请求
			uni.request(Object.assign({}, _config, options));
		}).catch(err => {
			uni.hideLoading()
			_page_error(err.statusCode)
			reject(err)
		})
    })
}

// request 错误
function _page_error(err) {
    switch (err) {
        case 401:
            uni.showToast({
                title: '登录超时了，请重新登录',
                icon: 'none',
                duration: 2000
            });
            break;
        case 404:
            // 错误码404的处理方式
            uni.showToast({
                title: '连接服务器出了点小问题，请稍后再试',
                icon: 'none',
                duration: 2000
            });
            break;
		case 405:       
            uni.showToast({
                title: '连接服务器出了点小问题，请稍后再试',
                icon: 'none',
                duration: 2000
            });
            break;
		case 503:
			  uni.showToast({
				 title: '服务不可用',
				 icon: 'none',
				 duration: 2000
			 });
			break;
		default : 
			uni.showToast({
				title: '加载失败',
				icon: 'none'
			})
    }
}